package cn.ms.doctor.Service;

import cn.ms.doctor.Entity.User;
import cn.ms.doctor.Mapper.DeptMapper;
import cn.ms.doctor.Mapper.PrescriptionMapper;
import cn.ms.doctor.Mapper.RoleMapper;
import cn.ms.doctor.Mapper.UserMapper;
import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService{
    @Resource
    private UserMapper userMapper;
    @Resource
    private DeptMapper deptMapper;
    @Resource
    private PrescriptionMapper prescriptionMapper;
    @Resource
    private RoleMapper roleMapper;
    @Override
    public User getUserByAccountAndPasswordAndRole(Map<String, Object> params) {
        return userMapper.getUserByAccountAndPasswordAndRole(params);
    }

    @Override
    public Boolean updateUserNameByUserId(Integer userId, String userName) {
        return userMapper.updateUserNameByUserId(userId,userName);
    }

    @Override
    public boolean updateUserPasswordByUserId(Integer userId, String newPassword) {
        return userMapper.updateUserPasswordByUserId(userId,newPassword);
    }

    @Override
    public Map<String, Object> getUsersBySelective(Map<String, Object> params) {
        Map<String, Object> data=new HashMap<>();
        Page<Object> page = PageHelper.startPage((Integer) params.get("pageIndex"), (Integer) params.get("pageSize"));
        List<Map<String, Object>> userData = userMapper.getUsersByUserIdOrUserNameAndDeptId(params);
        for (Map<String, Object> userDatum : userData) {
            userDatum.put("deptName",deptMapper.getDeptNameByDeptIdString((Integer) userDatum.get("deptId")));
            userDatum.put("prescriptionCount",prescriptionMapper.getPrescriptionCountByUserId((Integer) userDatum.get("userId")));
            userDatum.put("roleName",roleMapper.getRoleNameByRoleId((Integer) userDatum.get("roleId")));
        }
        data.put("pageData",userData);
        data.put("pageCount",page.getPages());
        data.put("pageIndex",page.getPageNum());
        data.put("pageSize",page.getPageSize());
        return data;
    }

    @Override
    public boolean updateRoleOrDeptOrName(Map<String, Object> params) {
        return userMapper.updateRoleOrDeptOrName(params);
    }

    @Override
    public boolean deleteUserByUserIdAndAccount(Map<String, Object> params) {
        return userMapper.deleteUserByUserIdAndAccount(params);
    }

    @Override
    public boolean addUser(Map<String, Object> params) {

        return userMapper.addUser(params);
    }
}
